package Day2025_03_24;

public class LeetCode474 {
    public int findMaxForm(String[] strs, int m, int n) {
        int sn = strs.length;
        int[][][] dp = new int[sn + 1][m + 1][n + 1];
        for (int i = 1; i <= sn; i++) {
            int a = calc(strs[i - 1])[0], b = calc(strs[i - 1])[1];
            for (int j = 0; j <= m; j++) {
                for (int k = 0; k <= n; k++) {
                    dp[i][j][k] = dp[i - 1][j][k];
                    if (j >= a && k >= b) dp[i][j][k] = Math.max(dp[i][j][k], dp[i - 1][j - a][k - b] + 1);
                }
            }
        }
        return dp[sn][m][n];
    }

    private int[] calc (String s) {
        int[] ret = new int[2];
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '0') ret[0]++;
            else ret[1]++;
        }
        return ret;
    }
}
